package com.tiantianfresh.admin.controller;

import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.Role;
import com.tiantianfresh.admin.service.RoleService;
import com.tiantianfresh.admin.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author FANGAO
 * @date 2021/11/13 - 下午 16:48
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @ResponseBody
    @RequestMapping("/index")
    public Object index(String queryText,Integer currentPageNo,Integer pageSize){
        AJAXResult ajaxResult  = new AJAXResult();
        try {
            Page<Role> page = new Page<Role>(pageSize,currentPageNo,roleService.roleTotalCount(queryText));
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("currentPageNo",page.getCurrentPageNo());
            map.put("pageSize",page.getPageSize());
            map.put("name",queryText);
            page.setDatas(roleService.getListRole(map));
            ajaxResult.setData(page);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @RequestMapping("/add")
    public String add(){
        return "/role/add";
    }

    @ResponseBody
    @RequestMapping("/insert")
    public Object insert(Role role){
        AJAXResult result = new AJAXResult();
        try {
            result.setSuccess(roleService.insert(role));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
    @RequestMapping(value ="/queryByid/{id}")
    public String queryByid(@PathVariable Integer id, Model model){
        Role role = roleService.queryByid(id);
        model.addAttribute("role",role);
        return "role/edit";
    }
    @ResponseBody
    @RequestMapping("/update")
    public Object roleUpdate(Role role){
        AJAXResult result = new AJAXResult();
        try {
            result.setSuccess(roleService.roleUpdate(role));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/detele")
    public Object roleDelete(Integer id){
        AJAXResult result = new AJAXResult();
        try {
            result.setSuccess(roleService.roleDelete(id));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/deleteRoles")
    public Object deleteRoles(Integer[] roleid){
        AJAXResult result = new AJAXResult();
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("roleids",roleid);
            result.setSuccess(roleService.deleteRoles(map));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
    @RequestMapping("/assign")
    public String assign(){
        return "role/assign";
    }
    @ResponseBody
    @RequestMapping("/doArrign")
    public Object doArrign(Integer roleid,Integer[] permissionids){
        AJAXResult result = new AJAXResult();
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("roleid",roleid);
            map.put("permissionids",permissionids);
            //给角色添加许可前，先删除已有的许可防止重复添加
            roleService.deleteRolePermission(map);
            result.setSuccess(roleService.insertRolePermission(map));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

}
